//
//  PacketModel.m
//  Fotile
//
//  Created by AllenKwok on 16/5/24.
//  Copyright © 2016年 xlink.cn. All rights reserved.
//

#import "PacketModel.h"

@implementation PacketModel

- (instancetype)init{
    if (self = [super init]) {
        _header = 0xf4f5;
        _flag = 0;
        _status = 1;
        _type = 0;
        _command = 0;
        _len = 8;
    }
    return self;
}

//- (void)setPayload:(NSData *)payload{
//    _payload = payload;
//    _len = _payload.length + 8;
//}
//仅对"需要计算长度和修改的参数"进行二级制转化
-(void)configInPayload:(NSData *)payload{
    uint8_t num[payload.length];
    [payload getBytes:&num length:payload.length];
    
    NSMutableData *afterData = [NSMutableData data];
    BOOL isAdd = NO;
    for (int i = 0; i<payload.length; i++) {
        uint8_t a = num[i];
        [afterData appendBytes:&a length:1];
        if (a==0xf4 && self.type == DeviceTypeCooker) {
            // 只有灶具才会加这一句协议
            [afterData appendBytes:&a length:1];
            isAdd = YES;
        }
    }
    if (isAdd) {
        NSLog(@"===========已经转义===%@===%@",payload,afterData);
    }
//    free(num);
    
    _payload = afterData;
    _len = _payload.length + 8;
}

-(unsigned char)getCheckCode{
    unsigned short checkLen = 2 + 2 + 2 + 1 + 1 + 2 + _payload.length;
    char checkData[checkLen];
    
    memcpy(checkData, &_header, 2);
    memcpy(checkData + 2, &_len, 2);
    memcpy(checkData + 2 + 2, &_type, 2);
    memcpy(checkData + 2 + 2 + 2, &_command, 1);
    memcpy(checkData + 2 + 2 + 2 + 1, &_status, 1);
    memcpy(checkData + 2 + 2 + 2 + 1 + 1, &_flag, 2);
    [_payload getBytes:checkData + 2 + 2 + 2 + 1 + 1 + 2  length:_payload.length];
    
    unsigned char checkCode = checkData[0];
    for (unsigned short i = 1; i < checkLen; i++) {
        checkCode ^= checkData[i];
    }
    
    return checkCode;
}
//APP SDK发送数据最后拼接方法
- (NSData *)getData{
    uint16_t temp = 0;
    NSMutableData *data = [NSMutableData data];
    temp = _header;
    HTONS(temp);
    [data appendBytes:&temp length:2];
    temp = _len;
    HTONS(temp);
    [data appendBytes:&temp length:2];
    temp = _type;
//    HTONS(temp);
    [data appendBytes:&temp length:2];
    [data appendBytes:&_command length:1];
    [data appendBytes:&_status length:1];
    temp = _flag;
    HTONS(temp);
    [data appendBytes:&temp length:2];
    [data appendData:_payload];
    unsigned short checkCode = [self getCheckCode];
    temp = HTONS(checkCode);
    [data appendBytes:&checkCode length:2];
    return data;
}

-(instancetype)initWithPacketData:(NSData *)packetData{
    if (self = [self init]) {
        uint16_t temp=0;
        [packetData getBytes:&temp range:NSMakeRange(0, 2)];
        _type =HTONS(temp);
        [packetData getBytes:&_deviceType range:NSMakeRange(0, 1)];
        [packetData getBytes:&_deviceModel range:NSMakeRange(1, 1)];
        [packetData getBytes:&_command range:NSMakeRange(2, 1)];
        [packetData getBytes:&_status range:NSMakeRange(3, 1)];
        [packetData getBytes:&_flag range:NSMakeRange(4, 2)];
        _payload = [packetData subdataWithRange:NSMakeRange(6, packetData.length-8)];
        
        uint8_t num[_payload.length];
        [_payload getBytes:&num length:_payload.length];
        if (_deviceType == DeviceTypeCooker) {//现在暂时只需要处理灶具的连续0xf4
            NSMutableData *data = [NSMutableData data];
            for (int i = 0; i<_payload.length; i++) {
                uint8_t n = num[i];
                [data appendBytes:&n length:1];
                if (n == 0xf4 && i < _payload.length - 1) {//去除连续的0xf4
                    if (num[i+1] == 0xf4) {
                        i++;
                        NSLog(@"有连续的0xF4 ---> %d",i);
                    }
                }
            }
            _payload = data;
        }
        
        [packetData getBytes:&_checkCode range:NSMakeRange(packetData.length-1, 1)];
    }
    return self;
}

- (BOOL)veriflcation{
    unsigned char checkCode = [self getCheckCode];
    if (checkCode == _checkCode) {
        return true;
    }else{
        return false;
    }
}

-(NSString *)description{
    return [NSString stringWithFormat:@"TYPE:%04X,CMD:%02X,STAT:%02X,FLAG:%04X,CHECHCODE:%04X,payload:%@",_type,(int)_command,_status,_flag,_checkCode,_payload];
}

@end
